<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    exclude-result-prefixes="xd xsi"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rifcs="http://ands.org.au/standards/rif-cs/registryObjects">

    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xd:doc scope="stylesheet">
        <xd:desc>
            <xd:p><xd:b>Created on:</xd:b> Oct 10, 2011</xd:p>
            <xd:p><xd:b>Author:</xd:b> King Lung Chiu</xd:p>
            <xd:p>
                Converts a valid RIF-CS document to its
                equivalent in muRIFCS schema.

                Works by pulling every activity, collection,
                party and service elements from the source document
                and wrapping them with the muRIFCS
                    <root fromLab="...">
                        <rifcsData>
                            ...
                        </rifcsData>
                    </root>
                wrapper.

                The first registryObject/@group value from the source
                document is used as the value for /root/@fromLab.
                Every RIF-CS element pulled from the source document
                is assumed to belong to the same group.

                All other data from the source document's registryObjects
                and registryObject elements are discarded.
            </xd:p>
        </xd:desc>
    </xd:doc>

    <xsl:param name="registryObjectGroup" select="//rifcs:registryObject[@group][1]/@group"/>


    <xsl:template match="/">
        <mu:root
            fromLab="{$registryObjectGroup}"
            xmlns:mu="http://rdr.unimelb.edu.au/schema/murifcs"
            xmlns:rifcs="http://ands.org.au/standards/rif-cs/registryObjects"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation=
            "http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/1.2.0/schema/registryObjects.xsd
            http://rdr.unimelb.edu.au/schema/murifcs http://rdr.unimelb.edu.au/schema/muRIFCS.xsd">

	<mu:rifcsData>
                <xsl:apply-templates select="rifcs:registryObjects/rifcs:registryObject"/>
            </mu:rifcsData>
         </mu:root>
    </xsl:template>

    <xsl:template match="rifcs:registryObject">
        <xsl:copy-of select="rifcs:activity | rifcs:collection | rifcs:party | rifcs:service"/>
    </xsl:template>

</xsl:stylesheet>